package com.shanzmoo.h5.domain.dto;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.shanzmoo.base.annotation.IntIn;
import com.shanzmoo.base.bean.ValidList;
import com.shanzmoo.base.constant.RegularExpression;
import com.shanzmoo.base.util.BillNoUtils;
import com.shanzmoo.db.mall.entity.MallOrderEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.constraints.*;
import java.io.Serializable;
import java.math.BigDecimal;

/**
 * 商城订单保存信息
 *
 * @author Gzhao 2020/12/25
 */
@Data
@ApiModel("商城订单保存信息")
public class MallOrderSaveDTO implements Serializable {

    private static final Long serialVersionUID = 1758L;

    @DecimalMin(value = "0.00", message = "请输入正确的运费")
    @DecimalMax(value = "1000.00", message = "运费超出最大范围")
    @NotNull(message = "运费不允许为空")
    @ApiModelProperty(name = "运费", example = "9.90")
    private BigDecimal freight;

    @NotBlank(message = "收货人不允许为空")
    @ApiModelProperty(name = "收货人", example = "铁手")
    private String contacts;

    @NotBlank(message = "联系方式不允许为空")
    @ApiModelProperty(name = "联系方式", example = "13645624562")
    private String mobile;

    @NotBlank(message = "收货地区编号不允许为空")
    @ApiModelProperty(name = "收货地区编号(第三级地区编码)", example = "420100")
    private String districtCode;

    @NotBlank(message = "收货地址名不允许为空")
    @ApiModelProperty(name = "收货地址名", example = "湖北省 武汉市 武昌区")
    private String districtStr;

    @NotBlank(message = "详细地址不允许为空")
    @ApiModelProperty(name = "详细地址", example = "光谷国际广场")
    private String address;

    @ApiModelProperty(name = "发票单位名称（个人名称）", example = "XXXXXX")
    private String invoiceTitle;

    @ApiModelProperty(name = "发票识别号（手机号码）", example = "123456789")
    private String invoiceIdentification;

    @Pattern(regexp = RegularExpression.EMAIL, message = "请输入正确的邮箱格式")
    @ApiModelProperty(name = "发票邮箱", example = "123@qq.com")
    private String invoiceEmail;

    @IntIn(values = {0,1,2}, message = "请传入正确的发票内容")
    @ApiModelProperty(name = "发票内容：0-购物明细，1-数据电子，2-3c类型", example = "2")
    private Integer invoiceContentType;

    @ApiModelProperty(name = "买家留言")
    private String memberRemark;

    @NotEmpty(message = "商城订单商品明细不允许为空")
    @ApiModelProperty(name = "商城订单商品明细")
    private ValidList<MallOrderDetailSaveDTO> orderDetailList;

    public MallOrderEntity convert(String memberId, String username) {
        MallOrderEntity mallOrderEntity = JSON.parseObject(JSON.toJSONString(this), MallOrderEntity.class);
        mallOrderEntity.setMemberId(Integer.parseInt(memberId));
        mallOrderEntity.setUsername(username);
        mallOrderEntity.setStatus(0);
        mallOrderEntity.setPayStatus(false);
        mallOrderEntity.setOrderNo(BillNoUtils.generateMallOrderNo());

        if (StrUtil.isNotBlank(invoiceTitle)) {
            mallOrderEntity.setInvoiceType(0);
        }
        return mallOrderEntity;
    }

}
